﻿using DAO;
using DTO;
using System;
using System.Data;
using System.Web;
using Common;
namespace BUS
{
    public class NguoiDungBUS
    {
        DataProvider dao = new DataProvider();
        public bool DangKi(NguoiDungDTO nguoidungdto)
        {
            object[] para = { "@Email", nguoidungdto.Email, "@MatKhau",
                                MaHoa.MaHoaMD5(nguoidungdto.MatKhau), "@SDT", nguoidungdto.Email ,"@DiaChi",
                                nguoidungdto.DiaChi,"@HoTen",nguoidungdto.Hoten,"@NgayDangKi",nguoidungdto.NgayDangKi,"@ChinhSuaCuoi",nguoidungdto.LastChange,
                                "@TrangThai",nguoidungdto.TrangThai,"@MaPQ",nguoidungdto.MaPQ
                            };
            string query = "tbl_NguoiDung_DangKi";
            bool kq = dao.ExecuteQuery(query, CommandType.StoredProcedure, para);
            return kq;
        }
        public DataTable DangNhap(NguoiDungDTO nguoidungdto)
        {

            string mk = MaHoa.MaHoaMD5(nguoidungdto.MatKhau);
            object[] para = { "@Email", nguoidungdto.Email, "@MatKhau", mk };
            DataTable dt = dao.LayDuLieuThamSo("tbl_NguoiDung_DangNhap", CommandType.StoredProcedure, para);

            if (dt.Rows.Count > 0)
            {
                nguoidungdto.Email = dt.Rows[0]["Email"].ToString();
                nguoidungdto.MatKhau = dt.Rows[0]["MatKhau"].ToString();
                nguoidungdto.Hoten = dt.Rows[0]["HoTen"].ToString();
                nguoidungdto.DiaChi = dt.Rows[0]["DiaChi"].ToString();
                nguoidungdto.SDT = dt.Rows[0]["SDT"].ToString();
                try { nguoidungdto.NgayDangKi = Convert.ToDateTime(dt.Rows[0]["NgayDangKi"].ToString()); }
                catch { }
                try { nguoidungdto.LastChange = Convert.ToDateTime(dt.Rows[0]["ChinhSuaCuoi"].ToString()); }
                catch { }
                try { nguoidungdto.TrangThai = Convert.ToBoolean(dt.Rows[0]["TrangThai"].ToString()); }
                catch { }             
                HttpContext.Current.Session["KhachHang"] = nguoidungdto;
            }
            return dt;
        }
      
        #region Quản Trị Người Dùng     
            public DataTable LayDSNguoiDung()
            {
                return dao.LayDuLieuThamSo("tbl_NguoiDung_LayDSNguoiDung", CommandType.StoredProcedure);
            }

            public DataTable DangNhapQuanTri(NguoiDungDTO nguoidungdto)
            {
                string mk = MaHoa.MaHoaMD5(nguoidungdto.MatKhau);
                object[] para = { "@Email", nguoidungdto.Email, "@MatKhau", mk };
                DataTable dt = dao.LayDuLieuThamSo("tbl_QuanTri_DangNhap", CommandType.StoredProcedure, para);

                if (dt.Rows.Count > 0)
                {
                    nguoidungdto.Email = dt.Rows[0]["Email"].ToString();
                    nguoidungdto.MatKhau = dt.Rows[0]["MatKhau"].ToString();
                    nguoidungdto.Hoten = dt.Rows[0]["HoTen"].ToString();
                    nguoidungdto.DiaChi = dt.Rows[0]["DiaChi"].ToString();
                    nguoidungdto.SDT = dt.Rows[0]["SDT"].ToString();
                    nguoidungdto.NgayDangKi = Convert.ToDateTime(dt.Rows[0]["NgayDangKi"].ToString());
                    nguoidungdto.LastChange = Convert.ToDateTime(dt.Rows[0]["ChinhSuaCuoi"].ToString());
                    nguoidungdto.TrangThai = Convert.ToBoolean(dt.Rows[0]["TrangThai"].ToString());
                    nguoidungdto.MaPQ = Convert.ToInt32(dt.Rows[0]["MaPQ"].ToString());
                    HttpContext.Current.Session["QuanTri"] = nguoidungdto;
                }
                return dt;
            }
            public bool CapNhapNguoiDung(NguoiDungDTO objND)
            {
                object[] para = { "@TrangThai", objND.TrangThai, "@MaPQ", objND.MaPQ, "@ID", objND.ID };
                string query = "tbl_NguoiDung_QuanlyNguoiDung";
                bool kq = dao.ExecuteQuery(query, CommandType.StoredProcedure, para);
                return kq;
            }
            public DataTable LayDuLieuNguoiDung(int mand)
            {
                object[] para = { "@ID", mand };

                return dao.LayDuLieuThamSo("tlb_NguoiDung_LayDuLieuNguoiDung", CommandType.StoredProcedure, para);

            }
        #endregion

    }
}